// import { storage } from '@/utils/Storage';

import mqtt from './mqtt';

import options from './../childProcess/readOptions';

import factoryConnect from './../factoryConnect';

// console.log(options)

const isDev = process.env.NODE_ENV === 'development';

export const connectType: any = isDev
  ? 'plc'
  : options?.connectType || 'webSocket'; // webSocket

// export const connectType: any = 's7'; // webSocket

export const factoryName: any = isDev
  ? 'zhuohang'
  : options?.factoryName || 'dongfang';

export const factoryChildName: any = isDev
  ? 'zh20230829'
  : options?.factoryChildName ||
    'home';

// export const isConnectMqtt = isDev ? false :
//   storage.get('isConnectMqtt',null) !== null
//     ? storage.get('isConnectMqtt',null)
//     : mqttConnect[factoryName]?.isConnectMqtt || false;

// export const mqttOptions = isDev
//   ? null
//   : storage.get('mqttOptions', null) ||
//     mqttConnect[factoryName]?.options ||
//     null;



// const { protocol = null } = window.location;

export const mqttOptions = isDev
  ? null
  : options?.mqttOptions ||
    mqtt[factoryName]?.options ||
    null;

export const isConnectMqtt = isDev
  ? false
  : options?.isConnectMqtt ||
    mqtt[factoryName]?.isConnectMqtt ||
    false;

export const isUploadMessage = isDev
  ? false
  : options?.isUploadMessage || false;

// export const uploadWsUrl = isDev
//   ? `ws${urlPrefix?.includes('https:') ? 's' : ''}:${BASE_URL}/ws/rep/` +
//     '${machineCode}'
//   : options?.uploadWsUrl ||
//     `ws${urlPrefix?.includes('https:') ? 's' : ''}:${BASE_URL}/ws/rep/` +
//       '${machineCode}';

export const connectOptionsList = isDev
  ? factoryConnect[factoryName][factoryChildName]
  : options?.connectOptionsList ||
  factoryConnect[factoryName][factoryChildName];

if (!isDev && connectOptionsList && connectOptionsList.length) {
  for (const item of connectOptionsList) {
    if (
      (!item.addressList || item.addressList.length === 0) &&
      !item.addressObj &&
      factoryConnect[factoryName][factoryChildName] &&
      factoryConnect[factoryName][factoryChildName].length
    ) {
      const obj = factoryConnect[factoryName][factoryChildName].find(
        fItem => fItem.plcId === item.plcId
      );

      if (obj) {
        if (obj.addressList && obj.addressList.length) {
          item.addressList = obj.addressList;
        }

        if (obj.addressObj) {
          item.addressObj = obj.addressObj;
        }
      }
    }
  }
}
